<template>
	<view>
		<view class="loginForm hasMargTop">
			<template v-if="opt != 'regAcc'">
				<view class="loginItem hasFlex posRel">
					<view class="iconView">
						<view class="iconfont icon-zhanghao"></view>
					</view>
					<template v-if="setPwdLoginStatus == 0">
						<input type="text" maxlength="20" @blur="getMobileByUserAcc" class="input" :class="[isClickFlag ? 'accDis' : '']" :disabled="isClickFlag" name="account" v-model="account" placeholder="请输入账号或身份证号" placeholder-style="font-weight:normal;color:#aeaeae;"/>
					</template>
					<template v-else-if="setPwdLoginStatus == 1">
						<view class="input disBg">
							<text class="jmTxt">{{account}}</text>
						</view>
					</template>
				</view>
				<view class="loginItem hasFlex posRel">
					<view class="iconView">
						<view class="iconfont icon-shoujihao"></view>
					</view>
					<template v-if="setPwdLoginStatus == 0 || (setPwdLoginStatus == 1 && jmMobile != '')">
						<view class="input disBg">
							<template v-if="jmMobile == ''">
								<text class="disTxt">手机号码由账号或身份证号动态获取</text>
							</template>
							<template v-else>
								<text class="jmTxt">{{jmMobile}}</text>
							</template>
						</view>
					</template>
					<template v-if="setPwdLoginStatus == 1 && jmMobile == ''">
						<input type="tel" maxlength="11" class="input" name="phoneNum" v-model="phoneNum" placeholder="请输入手机号码" placeholder-style="font-weight:normal;color:#aeaeae;"/>
					</template>
				</view>
			</template>
			<template v-else>
				<view class="loginItem hasFlex posRel">
					<view class="iconView">
						<view class="iconfont icon-shoujihao"></view>
					</view>
					<input type="tel" maxlength="11" class="input" name="phoneNum" v-model="phoneNum" placeholder="请输入手机号码" placeholder-style="font-weight:normal;color:#aeaeae;"/>
				</view>
			</template>
			<view class="loginItem hasFlex posRel vercodeItem">
				<view class="iconView">
					<view class="iconfont icon-yanzhengma"></view>
				</view>
				<input type="number" class="input" name="vercode" v-model="vercode" placeholder="请输入验证码" placeholder-style="font-weight:normal;color:#aeaeae;"/>
				<view class="getVercode">
					<view class="lineView"></view>
					<block v-if="isClickFlag">
						<view class="againTxt">重新获取({{countData}}s)</view>
					</block>
					<block v-else>
						<view class="getTxt" @tap="getVercode">获取验证码</view>
					</block>
				</view> 
			</view>
			<view class="loginItem hasFlex">
				<view class="iconView">
					<view class="iconfont icon-mima"></view>
				</view>
				<input type="password" class="input" name="password" maxlength="15" v-model="password" placeholder="请输入密码" placeholder-style="font-weight:normal;color:#aeaeae;"/>
			</view>
			<view class="tips">注:1.密码由6-15位数字,大小写字母或符号构成</view>
			<view class="tips indent">2.获取验证码期间账号或身份证号禁止编辑</view>
			<view class="tips indent" v-if="setPwdLoginStatus == 0">3.如账号未绑定手机号码,请联系平台客服重置该账号密码</view>
			<button class="loginBtn" :disabled="isDisabled" @tap="goSave">{{currBtnTxt}}</button>
		</view>
	</view>
</template>

<script>
	var count = 60;
	import { sendMsgCode,goReg,upPwd,getUserMobileByUserAcc } from '@/api/authLogin'
	export default {
		data(){
			return{
				currBtnTxt:'',
				account:'',
				jmMobile:'',
				phoneNum:'',
				vercode:'',
				opt:'',
				isClickFlag :  false,
				countData : 60,
				timer : null,
				password:'',
				isDisabled:false,
				accDisFlag:false,
				setPwdLoginStatus:-1//0未登录设置 1已登录设置
			}
		},
		onLoad(options){
			var title = '';
			this.opt = options.opt;
			if(this.opt == 'regAcc'){
				title = '普通用户注册';
				this.currBtnTxt = '注册';
			}else{
				this.setPwdLoginStatus = options.loginStatus;
				title = '设置密码';
				this.currBtnTxt = '设置密码';
				if(this.setPwdLoginStatus == 1){
					let reg = /(\d{3})\d{4}(\d{4})/;
					this.account = options.account;
					if(options.mobile != ''){
						this.phoneNum = options.mobile;
						this.jmMobile = options.mobile.replace(reg, "$1****$2");
					}
				}
			}
			setTimeout(()=>{
				uni.setNavigationBarTitle({
					title
				})
			},300)
		},
		onUnload(){
			clearInterval(this.timer);
			count = 60;
		},
		methods:{
			getMobileByUserAcc(e){
				if(e.detail.value == '')return;
				let field = { account:e.detail.value };
				getUserMobileByUserAcc(field).then(res=>{
					let tmpRes = res.data.datas;
					this.jmMobile = tmpRes.tmMobile;
					this.phoneNum = tmpRes.mobile;
					if(this.phoneNum == ''){
						this.util.showToast('该账号未绑定手机号码,请联系平台客服重置该账号密码',5000);
					}
				}).catch(err=>{
					this.jmMobile = '';
					this.phoneNum = '';
					//console.log(err);
				})
			},
			//获取验证码
			getVercode(){
				if(this.phoneNum == ''){
					this.util.showToast('请输入手机号码');
				}else if(!this.regX.regPhone.test( this.phoneNum )){
					this.util.showToast('手机号码格式不对');
				}else{
					this.sendMsgCode();
				}
			},
			sendMsgCode(){
				uni.showLoading({title:'发送中...', mask: true });
				var tmpOpt = this.opt == 'regAcc' ? 'reg' : '';
				var content = {mobile:this.phoneNum,sign:"hxWL_@1$2$3",opt:tmpOpt};
				var contentStr = this.crypto.encrypt(JSON.stringify( content ));
				var field = {content:contentStr}
				sendMsgCode(field).then(res=>{
					this.util.showToastSuc('发送成功');
					this.isClickFlag = true;
					this.timer = setInterval(()=>{
						count-=1;
						this.countData = count;
						if(this.countData == 0){
							count = 60;
							clearInterval(this.timer);
							this.countData = 60;
							this.isClickFlag = false;
						}
					},1000);
				}).catch(err=>{
					console.log(err)
					if(err.data.code == 50003){
						this.util.showToast('该手机号已注册');
					}
				});
			},
			goSave(){
				if(this.phoneNum == ''){
					this.util.showToast('手机号码不能为空');
				}else if(!this.regX.regPhone.test( this.phoneNum )){
					this.util.showToast('手机号码格式不对');
				}else if(this.vercode == ''){
					this.util.showToast('请输入手机验证码');
				}else if(this.password == ''){
					this.util.showToast('请输入密码');
				}else if(this.password.length <6){
					this.util.showToast('密码最少为6位');
				}else if(this.password.length >15){
					this.util.showToast('密码最多为15位'); 
				}else if(this.regX.regCHN.test(this.password)){
					this.util.showToast('密码不能为中文');
				}else{
					var currTxt = this.opt == 'regAcc' ? '注册' : '设置';
					uni.showLoading({ title: currTxt + '中...', mask: true });
					this.isDisabled = true;
					var field = {mobile:this.phoneNum,inputCode:this.vercode,password:this.password};
					if(this.opt == 'regAcc'){
						var tmpField = { opt:'visitor' }
						Object.assign( field,tmpField );
						goReg(field).then(res=>{
							this.util.showToastSuc(currTxt + '成功');
							let tmpRes = res.data.datas;
							this.User.token = tmpRes.token;
							this.User.userIdPss = tmpRes.userId;
							uni.setStorageSync("token", this.User.token);
							uni.setStorageSync("userIdPss",this.User.userIdPss); 
							uni.setStorageSync('logAcc',tmpRes.account);
							uni.setStorageSync('userPortrait',tmpRes.userPortrait);
							uni.setStorageSync('nickName',tmpRes.netName);
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/index/index'
								})
							},1500)
						}).catch(err=>{
							this.isDisabled = false;
						})
					}else{
						let tmpField = { account:this.account };//2024-10-06新增
						Object.assign( field,tmpField );
						upPwd(field).then(res=>{
							this.succCallBack(currTxt);
						}).catch(err=>{
							this.isDisabled = false;
						})
					}
				}
			},
			succCallBack(currTxt){
				this.util.showToastSuc(currTxt + '成功');
				setTimeout(function(){
					uni.navigateBack({delta:1})
				},1500);
			}
		}
	}
</script>
<style lang="scss">
	page{background: #fff;}
</style>
<style lang='scss' scoped>
	@import '@/static/scss/login.scss';
	.hasMargTop{margin-top: 100rpx;}
	.tips{
		font-size: $font-size24;
		color: $pss-color-red;
		margin-top: 10rpx;
		opacity: .8;
		&.indent{
			text-indent: 30rpx;
		}
	}
</style>